<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="form" :inline="true" :model="formModel" :rules="rules">
        <el-form-item class="no-warn-item" label="当前工位:" prop="uloc">
          <el-input v-model="formModel.uloc" disabled/>
        </el-form-item>
        <el-form-item class="no-warn-item" label="工艺路线:" prop="tmBasRouteId">
          <el-select v-model="formModel.tmBasRouteId" :disabled="disabledRoute" clearable filterable>
            <el-option v-for="item in routeOptions" :key="item.tmBasRouteId" :label="item.routeName" :value="item.tmBasRouteId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="no-warn-item" label="打印模板名称" prop="modelFileName">
          <hey-select v-model="formModel.modelFileName" disabled clcd="print_model_cd" />
        </el-form-item>
        <el-row class="submit-area">
          <el-col :span="24">
            <el-button v-show="showSelect" type="primary" v-if="hasPermission('car_axle_queue_select')" plain @click="queryList(false)">查询</el-button>
            <el-button type="primary" v-if="hasPermission('car_axle_print')" plain @click="print">打印</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <hey-table ref="table" :table-data="tableData" :auto-width="false" :hasPagination="false" :columns="columns">

    </hey-table>
  </div>
</template>

<script>

  export default {
    name: 'CarAxlePrintList',
    components: {

    },
    props: {},
    data() {
      return {
        timer:null,
        disabledUloc:true,
        disabledRoute:true,
        showSelect:true,
        routeOptions:[],
        tableData:[],
        freshenTime:'',
        formModel: {
          tmBasRouteId:'',
          tmBasUlocId:'',
          ulocNo:'',
          uloc:'',
          modelFileName:'Car_Axle',
        },
        rules: {

        },
        columns: [
          { title: '车辆VIN号', key: 'vin'},
          { title: '工艺路线代码', key: 'routeNo'},
          { title: '工艺路线名称', key: 'routeName'},
          { title: '打印模板名称', key: 'modelFileName', clcd: 'print_model_cd' },
          { title: '是否已打印', key: 'isPrinted', clcd: 'ind_cd' },
          { title: '打印时间', key: 'printTime'},
          { title: '打印用户', key: 'printUsername'},
        ],
      }
    },
    computed: {

    },
    mounted() {
      this.$form = this.$refs.form
      this.initRules()
      //校验用户工位
      this.checkUserULoc()
      this.getFreshenTime()
    },
    beforeDestroy() {
      if(this.timer){
        clearInterval(this.timer)
      }
    },
    methods: {
      initRules() {
        const REQUIRED_MSG = this.CONST.REQUIRED_MSG
        this.rules = {
          tmBasRouteId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        }
        this.$nextTick(_ => {
          this.$form.clearValidate()
        })
      },
      checkUserULoc(){
        const param = {}
        this.get('/assembleQueue/checkUserUloc', param).then(res => {
          this.formModel.uloc = res[0].ulocNo + '-' +res[0].ulocName
          this.formModel.ulocNo = res[0].ulocNo
          this.formModel.tmBasUlocId = res[0].tmBasUlocId
          this.disabledRoute = false
          this.getRoute()
        })
      },
      getFreshenTime(){
        const param = {}
        param.paramKey = 'GP2'
        param.isDelete = '0'
        this.get('/globalParamsTbl/queryGlobalParamsTbl', param).then(res => {
          this.freshenTime = res.list[0].paramValue
        })
      },
      getRoute(){
        //通过当前工位编号查询所有的工艺路线
        const param1 = {pageNum:1,pageSize:9999}
        param1.isDelete = '0'
        param1.curUlocId = this.formModel.tmBasUlocId
        this.get('/tmBasRouteNode/queryTmBasRouteNode', param1).then(res => {
          this.routeOptions = res.list
        })
      },
      queryList() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.disabledRoute = true
            this.showSelect = false
            this.queryTable()
            this.timer = setInterval(this.queryTable, this.freshenTime);
          }
        })
      },
      print() {
        if(this.tableData.length==0){
          this.$warn('请先查询打印列表!')
          return false
        }
        const obj = this.tableData.find(item => item.isPrinted === '0')
        const msg = '确认打印VIN号为['+obj.vin+']的后桥分装指示单?'
        this.$sure(msg, _ => {
          const param = {}
          param.vin = obj.vin
          param.modelFileName = this.formModel.modelFileName
          this.post('/assemblePrintRecord/print', param).then(_ => {
            this.$info('打印成功!', this.queryTable)
          })
        })
      },
      queryTable(){
        const param = {}
        param.ulocNo = this.formModel.ulocNo
        param.tmBasRouteId = this.formModel.tmBasRouteId
        param.modelFileName = this.formModel.modelFileName
        this.$refs.table.query('/assemblePrintRecord/query', param, '', false)
      }
    }
  }
</script>


